home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-01 / xshp15.zip / FILLRECT.ASM < prev    next >
Assembly Source File  |  1991-04-02  |  5KB  |  114 lines

  1. ; Mode X (320x240, 256 colors) rectangle fill routine. Works on all
  2. ; VGAs. Uses fast approach that fans data out to up to four planes at
  3. ; once to draw up to four pixels at once. Fills up to but not
  4. ; including the column at EndX and the row at EndY. No clipping is
  5. ; performed.
  6. ; C near-callable as:
  7. ;    void FillRectangleX(int StartX, int StartY, int EndX, int EndY,
  8. ;       unsigned int PageBase, int Color);
  9.  
  10. SC_INDEX equ    03c4h   ;Sequence Controller Index
  11. MAP_MASK equ    02h     ;index in SC of Map Mask register
  12. SCREEN_SEG equ  0a000h  ;segment of display memory in mode X
  13. SCREEN_WIDTH equ 80     ;width of screen in bytes from one scan line
  14.                         ; to the next
  15. parms   struc
  16.         dw      2 dup (?) ;pushed BP and return address
  17. StartX  dw      ?       ;X coordinate of upper left corner of rect
  18. StartY  dw      ?       ;Y coordinate of upper left corner of rect
  19. EndX    dw      ?       ;X coordinate of lower right corner of rect
  20.                         ; (the row at EndX is not filled)
  21. EndY    dw      ?       ;Y coordinate of lower right corner of rect
  22.                         ; (the column at EndY is not filled)
  23. PageBase dw     ?       ;base offset in display memory of page in
  24.                         ; which to fill rectangle
  25. Color   dw      ?       ;color in which to draw pixel
  26. parms   ends
  27.  
  28.         .model  small
  29.         .data
  30. ; Plane masks for clipping left and right edges of rectangle.
  31. LeftClipPlaneMask       db      00fh,00eh,00ch,008h
  32. RightClipPlaneMask      db      00fh,001h,003h,007h
  33.         .code
  34.         public  _FillRectangleX
  35. _FillRectangleX proc    near
  36.         push    bp      ;preserve caller's stack frame
  37.         mov     bp,sp   ;point to local stack frame
  38.         push    si      ;preserve caller's register variables
  39.         push    di
  40.  
  41.         cld
  42.         mov     ax,SCREEN_WIDTH
  43.         mul     [bp+StartY] ;offset in page of top rectangle scan line
  44.         mov     di,[bp+StartX]
  45.         shr     di,1    ;X/4 = offset of first rectangle pixel in scan
  46.         shr     di,1    ; line
  47.         add     di,ax   ;offset of first rectangle pixel in page
  48.         add     di,[bp+PageBase] ;offset of first rectangle pixel in
  49.                         ; display memory
  50.         mov     ax,SCREEN_SEG   ;point ES:DI to the first rectangle
  51.         mov     es,ax           ; pixel's address
  52.         mov     dx,SC_INDEX ;set the Sequence Controller Index to
  53.         mov     al,MAP_MASK ; point to the Map Mask register
  54.         out     dx,al
  55.         inc     dx      ;point DX to the SC Data register
  56.         mov     si,[bp+StartX]
  57.         and     si,0003h                 ;look up left edge plane mask
  58.         mov     bh,LeftClipPlaneMask[si] ; to clip & put in BH
  59.         mov     si,[bp+EndX]
  60.         and     si,0003h                  ;look up right edge plane
  61.         mov     bl,RightClipPlaneMask[si] ; mask to clip & put in BL
  62.         
  63.         mov     cx,[bp+EndX]    ;calculate # of addresses across rect
  64.         mov     si,[bp+StartX]
  65.         cmp     cx,si
  66.         jle     FillDone        ;skip if 0 or negative width
  67.         dec     cx
  68.         and     si,not 011b
  69.         sub     cx,si
  70.         shr     cx,1
  71.         shr     cx,1    ;# of addresses across rectangle to fill - 1
  72.         jnz     MasksSet ;there's more than one byte to draw
  73.         and     bh,bl   ;there's only one byte, so combine the left
  74.                         ; and right edge clip masks
  75. MasksSet:
  76.         mov     si,[bp+EndY]
  77.         sub     si,[bp+StartY]  ;BX = height of rectangle
  78.         jle     FillDone        ;skip if 0 or negative height
  79.         mov     ah,byte ptr [bp+Color] ;color with which to fill
  80.         mov     bp,SCREEN_WIDTH ;stack frame isn't needed any more
  81.         sub     bp,cx   ;distance from end of one scan line to start
  82.         dec     bp      ; of next
  83. FillRowsLoop:
  84.         push    cx      ;remember width in addresses - 1
  85.         mov     al,bh   ;put left-edge clip mask in AL
  86.         out     dx,al   ;set the left-edge plane (clip) mask
  87.         mov     al,ah   ;put color in AL
  88.         stosb           ;draw the left edge
  89.         dec     cx      ;count off left edge byte
  90.         js      FillLoopBottom ;that's the only byte
  91.         jz      DoRightEdge ;there are only two bytes
  92.         mov     al,00fh ;middle addresses are drawn 4 pixels at a pop
  93.         out     dx,al   ;set the middle pixel mask to no clip
  94.         mov     al,ah   ;put color in AL
  95.         rep     stosb   ;draw the middle addresses four pixels apiece
  96. DoRightEdge:
  97.         mov     al,bl   ;put right-edge clip mask in AL
  98.         out     dx,al   ;set the right-edge plane (clip) mask
  99.         mov     al,ah   ;put color in AL
  100.         stosb           ;draw the right edge
  101. FillLoopBottom:
  102.         add     di,bp   ;point to the start of the next scan line of
  103.                         ; the rectangle
  104.         pop     cx      ;retrieve width in addresses - 1
  105.         dec     si      ;count down scan lines
  106.         jnz     FillRowsLoop
  107. FillDone:
  108.         pop     di      ;restore caller's register variables
  109.         pop     si
  110.         pop     bp      ;restore caller's stack frame
  111.         ret
  112. _FillRectangleX endp
  113.         end
  114.